﻿Public Class FRKelas

    'member variables
    Private _cKelas As CKelas

    'events
    Public Event DataTerupdate()

    Public Sub New(ByVal id As Integer, ByVal nama As String, ByVal tingkatan As Integer, ByVal kapasitas As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'Me.Text = My.Settings.NamaAplikasi & " [Rubah Kelas]"
        Me.Nama = nama
        Me.IDKelas = id
        Me.Tingkatan = tingkatan
        Me.Kapasitas = kapasitas

        'tampilkan ke control
        TBNama.Text = Me.Nama
        TBKapasitas.Text = Me.Kapasitas
        Me.CbTingkatan.Text = Me.Tingkatan
    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles LabelJudul.MouseDown, MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(cont As Control, g As Graphics, p As Pen)
        For Each ctl As Control In cont.Controls
            If ctl.Visible Then
                p.Color = ctl.BackColor
                If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                    g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
                ElseIf TypeOf ctl Is Panel Then
                    PaintTextBox(ctl, g, p)
                ElseIf TypeOf ctl Is GroupBox Then
                    PaintTextBox(ctl, g, p)
                End If
            End If

        Next
    End Sub

    Private Sub SetRegionTextBox(cont As Control)
        For Each ctl As Control In cont.Controls
            If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub


    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub


    '-----------------------------------------------------------------------------------------------
#End Region

    Public Property Nama As String
    Public Property IDKelas As Integer
    Public Property Kapasitas As Integer
    Public Property Tingkatan As Integer


    Private Sub TBNama_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBNama.TextChanged
        Me.Nama = TBNama.Text.Trim
    End Sub

    Private Sub CbTingkatan_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CbTingkatan.SelectedIndexChanged
        Me.Tingkatan = CbTingkatan.Text
    End Sub

    Private Sub TBKapasitas_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBKapasitas.TextChanged
        Me.Kapasitas = TBKapasitas.Text.Trim
    End Sub

    Private Sub BtnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSimpan.Click
        'validasi inputan
        Dim _isError As Boolean = False

        'validasi nama
        If TBNama.Text.Trim = "" Then
            'ganti warna background
            TBNama.BackColor = Color.Orange
            _isError = True
        End If

        'validasi tingkatan
        If CbTingkatan.Text.Trim = "" Then
            'ganti warna background
            CbTingkatan.BackColor = Color.Orange
            _isError = True
        End If

        'validasi kapasitas
        If TBKapasitas.Text.Trim = "" Then
            'ganti warna background
            TBKapasitas.BackColor = Color.Orange
            _isError = True
        End If

        'jika ada data kosong maka tampilkan pesan
        If _isError Then
            MessageBox.Show("Simpan gagal, data kosong.", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            'simpan data ke dalam database
            _cKelas = New CKelas
            _cKelas.rubah(TBNama.Text.Trim, CbTingkatan.Text, TBKapasitas.Text.Trim, Me.IDKelas)
            RaiseEvent DataTerupdate()
            Me.Close()
        End If
    End Sub

    Private Sub BtnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBatal.Click
        If MessageBox.Show("Anda mau keluar? jika ya data tidak akan disimpan.", "Peringatan", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub SimpanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        BtnSimpan.PerformClick()
    End Sub

    Private Sub BatalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BatalToolStripMenuItem.Click
        BtnBatal.PerformClick()
    End Sub

    Private Sub FRKelas_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)
    End Sub
End Class